package com.mbp.config;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Arrays;

/**
 * mybatis-plus代码生成器
 *
 * @author: 王霄
 * @date: 2021/10/12 12:54
 **/
public class Generator {

    //数据源
    private static final String URL = "jdbc:mysql://127.0.0.1:3306/dm";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "root";
    private static final String[] TABLE = {
            "dm_point_template_info",
    };

    public static void main(String[] args) {
        FastAutoGenerator.create(new DataSourceConfig.Builder(URL, USERNAME, PASSWORD))
                .globalConfig(builder -> builder
                        .outputDir(System.getProperty("user.dir") + "/src/main/java")
                        .disableOpenDir()
                        .author("admin")
                        .dateType(DateType.TIME_PACK)
                        .build())
                .packageConfig(builder -> builder
                        .parent("com.pig4cloud.pig.device")
                        .entity("api.entity")
                        .mapper("mapper")
                        .xml("xml")
                        .service("service")
                        .serviceImpl("service.impl")
                        .controller("controller")
                        .build())
                .strategyConfig(builder -> builder
                        .addInclude(TABLE)
                        .addTablePrefix(Arrays.asList("dm_"))
                        //entity构造
                        .entityBuilder()
                        .enableFileOverride()
                        .disableSerialVersionUID()
                        .enableLombok()
                        .enableChainModel()
                        .enableRemoveIsPrefix()
                        .enableTableFieldAnnotation()
                        .naming(NamingStrategy.underline_to_camel)
                        .columnNaming(NamingStrategy.underline_to_camel)
                        .idType(IdType.AUTO)
                        //mapper构造
                        .mapperBuilder()
                        .enableBaseResultMap()
                        .superClass(BaseMapper.class)
                        .formatMapperFileName("%sMapper")
                        .formatXmlFileName("%sMapper")
                        //service构造
                        .serviceBuilder()
                        .formatServiceFileName("%sService")
                        .formatServiceImplFileName("%sServiceImpl")
                        //controller构造
                        .controllerBuilder()
                        .enableHyphenStyle()
                        .enableRestStyle()
                        .formatFileName("%sController")
                        .build())
                .templateConfig(builder -> builder
                        .disable(TemplateType.CONTROLLER)
                        .build())
                .execute();
    }
}
